package com.ygjy.webapplication;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

/**
 * 描述：
 */
public abstract class BaseDialogFragment extends DialogFragment {
    public final String TAG = this.getClass().getSimpleName();
    protected Context mContext;
    protected boolean isKeycodeBack;//是否禁止物理返回

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initCreate();
        mContext = getContext();
    }

    protected void initCreate() {
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Theme_MaterialComponents_Light_NoActionBar_Transparent);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setDialog(getDialog());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mContext = null;
    }

    protected void setDialog(Dialog dialog) {
        if (dialog != null) {
            dialog.setOnKeyListener((dialog1, keyCode, event) -> {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    onKeycodeBackListener();
                    return isKeycodeBack;
                }
                return false;
            });

        }
    }

    protected void onKeycodeBackListener(){

    }


}
